package by.grsu.shakh.page.librarian;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.shakh.app.LibraryWebApplication;
import by.grsu.shakh.dao.UserDao;
import by.grsu.shakh.entity.User;

/**
 * list page for User object.
 * 
 * @author shakh_andrew
 */
public class ListUserPage extends WebPage {

	public ListUserPage() {
		super();

		final UserDao userDao = new UserDao(
				LibraryWebApplication.DATA_XML_FOLDER);

		final List<User> allUsers = userDao.getAll();

		add(new DataView<User>("table-rows", new ListDataProvider<User>(
				allUsers)) {
			@Override
			protected void populateItem(final Item<User> item) {
				final User user = item.getModelObject();

				item.add(new Label("id", user.getId()));
				item.add(new Label("password", user.getPassword()));
				item.add(new Label("firstName", user.getFirstName()));
				item.add(new Label("surName", user.getSurName()));
				item.add(new Label("adress", user.getAdress()));
				item.add(new Label("mail", user.getMail()));
				item.add(new Label("role", user.getUserRole()));
				item.add(new Label("login", user.getLogin()));

				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new UserEditPage(user));
					}
				});

				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						userDao.delete(user.getId());
						setResponsePage(new ListUserPage());
					}
				});
			}
		});

		add(new Link("createLink") {
			@Override
			public void onClick() {
				setResponsePage(new UserEditPage(new User()));
			}
		});
	}
}
